package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.PowerChangeMonitorConfig;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.PowerChangeMonitorConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/2/20 9:13
 * 换电页面监控配置
 */
@RestController
@RequestMapping("/power/change/config")
public class PowerChangeMonitorConfigController extends BaseController {

    @Resource
    private PowerChangeMonitorConfigService monitorConfigService;


    /**
     * 根据区域id获取换电监控配置
     *
     * @param accountId 区域id
     * @return 换电监控配置
     */
    @GetMapping("/{accountId}")
    public Result<PowerChangeMonitorConfig> getByAccountId(@PathVariable("accountId") Integer accountId) {
        PowerChangeMonitorConfig monitorConfig = Optional.ofNullable(monitorConfigService.getById(accountId))
                .orElseGet(() -> new PowerChangeMonitorConfig().putDefaultValue(accountId));
        monitorConfig.checkNull();
        return Result.success(monitorConfig);
    }

    /**
     * 根据区域id获取换电监控配置
     *
     * @param config 。
     * @return 换电监控配置
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody @Validated PowerChangeMonitorConfig config) {
        monitorConfigService.updateById(config);
        config.intervalValueValid();
        return Result.success();
    }


}
